

package com.maoshi.shop.delivery.multishop.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.maoshi.shop.bean.enums.OfflineHandleEventType;
import com.maoshi.shop.bean.model.OfflineHandleEvent;
import com.maoshi.shop.bean.param.OfflineHandleEventAuditParam;
import com.maoshi.shop.common.annotation.SysLog;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.delivery.comment.model.Fetch;
import com.maoshi.shop.delivery.comment.service.FetchService;
import com.maoshi.shop.security.multishop.util.SecurityUtils;
import com.maoshi.shop.service.OfflineHandleEventService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;

/**
 * 自提点
 *
 * @author YXF
 * @date 2020-04-23 15:18:29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/admin/fetch" )
public class FetchController {

    private final FetchService fetchService;

    private final OfflineHandleEventService offlineHandleEventService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param fetch 自提点
     * @return 分页数据
     */
    @GetMapping("/page" )
    public ResponseEntity<IPage<Fetch>> getFetchPage(PageParam<Fetch> page, Fetch fetch) {
        return ResponseEntity.ok(fetchService.page(page, new LambdaQueryWrapper<Fetch>()
                .eq(Fetch::getShopId,SecurityUtils.getShopUser().getShopId())
                .eq(!Objects.isNull(fetch.getStatus()),Fetch::getStatus,fetch.getStatus())
                .like(StrUtil.isNotBlank(fetch.getFetchName()),Fetch::getFetchName,fetch.getFetchName())
                .orderByDesc(Fetch::getStatus)
        ));
    }


    /**
     * 通过id查询自提点
     * @param fetchId id
     * @return 单个数据
     */
    @GetMapping("/info/{fetchId}" )
    public ResponseEntity<Fetch> getById(@PathVariable("fetchId") Long fetchId) {
        return ResponseEntity.ok(fetchService.getById(fetchId));
    }

    /**
     * 新增自提点
     * @param fetch 自提点
     * @return 是否新增成功
     */
    @SysLog("新增自提点" )
    @PostMapping
    public ResponseEntity<Boolean> save(@RequestBody @Valid Fetch fetch) {
        fetch.setShopId(SecurityUtils.getShopUser().getShopId());
        Date date = new Date();
        fetch.setCreateTime(date);
        fetch.setUpdateTime(date);
        return ResponseEntity.ok(fetchService.save(fetch));
    }

    /**
     * 修改自提点
     * @param fetch 自提点
     * @return 是否修改成功
     */
    @SysLog("修改自提点" )
    @PutMapping
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid Fetch fetch) {
        fetch.setUpdateTime(new Date());
        return ResponseEntity.ok(fetchService.updateById(fetch));
    }

    /**
     * 通过id删除自提点
     * @param fetchId id
     * @return 是否删除成功
     */
    @SysLog("删除自提点" )
    @DeleteMapping("/{fetchId}" )
    public ResponseEntity<Boolean> removeById(@PathVariable Long fetchId) {
        return ResponseEntity.ok(fetchService.removeById(fetchId));
    }

    /**
     * 通过活动id获取下线信息
     */
    @GetMapping("/getOfflineHandleEventByFetchId/{fetchId}")
    public ResponseEntity<OfflineHandleEvent> getOfflineHandleEventByFetchId(@PathVariable("fetchId") Long fetchId) {
        OfflineHandleEvent offlineHandleEvent = offlineHandleEventService.getProcessingEventByHandleTypeAndHandleId(OfflineHandleEventType.FETCH.getValue(), fetchId);
        return ResponseEntity.ok(offlineHandleEvent);
    }
    /**
     * 违规商品提交审核
     */
    @PostMapping("/auditApply")
    public ResponseEntity<Void> auditApply(@RequestBody OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        Fetch seckill = fetchService.getById(offlineHandleEventAuditParam.getHandleId());
        if (seckill == null) {
            throw new MaoshiShopBindException("未找到该自提点信息");
        }
        fetchService.auditApply(offlineHandleEventAuditParam.getEventId(), offlineHandleEventAuditParam.getHandleId(), offlineHandleEventAuditParam.getReapplyReason());

        // 移除缓存
//        seckillService.removeSeckillCacheById(seckill.getSeckillId());
        return ResponseEntity.ok().build();
    }

}
